package com.suanfa.tianxun;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tianxun
 * @date 2023/12/14 14:55
 * @desc 基线条件：递归结束条件
 * @desc 递归条件：循环调用本身的条件
 */
public class DiGui {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(10);
        list.add(15);
        System.out.println("集合元素的和是:"+sum(list));
        System.out.println("集合元素的最大值是:"+max(0,list));
        System.out.println("集合元素的数量是:"+number(list));
    }

    private static Integer number(List<Integer> list) {
     if(list.size()==0){
         return 0;
     }else{
         return 1+number(list.subList(1,list.size()));
     }
    }

    private static Integer max(Integer max,List<Integer> list) {
        if(list.size()==0){
            return max;
        }else if(max < list.get(0)){
            max = list.get(0);
        }
        return max(max,list.subList(1,list.size()));
    }

    private static Integer sum(List<Integer> list) {
        if (list.size() == 0) {
            return 0;
        } else {
            return list.get(0) + sum(list.subList(1,list.size()));
        }
    }

}
